<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\service\communityService;
use think\Db;
use think\db\Where;
class Community extends Common{
    //区域管理
    public function communityList(){
        //查询当前账号下数据
        $com = $this->pidTokenId();
        //$com['community_id']=implode(",",$com['community_id']);
        $where=new Where();
        $where['id'] = array('in',$com['community_id']);
        $lists = (new communityService())->communityList($where);
        //查询对应物业名称
        foreach($lists as $lis){
            $lis['property'] = (new communityService())->propertyIdName($lis['property_id']);
            $list[] = $lis;
        }
        $number = count($lists);
        $this->assign('number',$number);
        if(!isset($list)){
            $this->assign('list',$lists);
        }else{
            $this->assign('list',$list);
        }
        return $this->fetch('community_list');
    }
    public function communityAdd(){
        //查询物业信息
        $com = $this->pidTokenId();
        $where=new Where();
        $where['id'] = array('in',$com['property_id']);
        $property = (new communityService())->propertyList($where);
        $this->assign('property',$property);
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            //判断账号名称是否重复
            $info = (new communityService())->communityName($data['name']);
            if($info){
                $this->error('区域已存在');
            }
            $data['time'] = date('Y-m-d H:i:s',time());
            //插入数据到数据库
            $result = (new communityService())->communityAdd($data);
            if($result){
                $this->success('添加区域成功',url('communityAdd'));
            }else{
                $this->error('添加区域失败',url('communityAdd'));
            }
        }
        return $this->fetch('community_add');
    }
    public function communityEdit(){
        //查询物业信息
        $com = $this->pidTokenId();
        $where['id'] = array('in',$com['property_id']);
        $property = (new communityService())->propertyList($where);
        $this->assign('property',$property);
        if(request()->isGet()){
            $id = request()->param('id');
            if(!isset($id) && empty($id)){
                $this->error('区域不存在');
            }
            $info = (new communityService())->communityId($id);
            $this->assign('info',$info);
            return $this->fetch('community_edit');
        }else{
            $data = request()->post();
            $result = (new communityService())->communityEdit($data);
            if($result){
                    $this->success('修改区域成功',url('communityList'));
            }else{
                    $this->error('修改区域失败');
            }
        }
    }
    public function communityDel(){
        $id = request()->param('id');
        $validate = $this->validate(request()->param(),[
            'id'	=>	'require|number'
        ]);
        if($validate !== true){
            $this->error($validate);
        }
        //判断是否存在该数据
        $info = (new communityService())->communityId($id);
        if(empty($info)){
            $this->error('该区域不存在');
        }
        $result =  (new communityService())->communityDel($id);
        if($result){
            $this->success('删除区域成功',url('communityList'));
        }else{
            $this->success('删除区域失败',url('communityList'));
        }
    }
}